package com.yanqu.road.dataHelper.action;

import com.yanqu.road.dataHelper.action.excel.UnionActivityServerMove;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.ActivityMgr;
import com.yanqu.road.dataHelper.manager.BaseMgr;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.manager.ServerMgr;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.rank.UnionActivityRank;
import com.yanqu.road.entity.rank.cross.CrossUnionActivityRank;
import com.yanqu.road.entity.servercenter.CrossServerInfo;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.union.UnionInfo;

import java.math.BigInteger;
import java.util.*;

public class UnionActivityMove extends actionBase {
    public void go(List<Long> serverList) {
        ActivityMgr activityMgr = BaseMgr.getMgr(ActivityMgr.class);
        Map<Long, ServerInfo> serverInfoMap = ServerMgr.getServerInfoList();
        activityMgr.initAllRunningActivity();
        List<ActivityInfo> activityInfoList = activityMgr.getAllRunningActivity();
        ServerMgr serverMgr = new ServerMgr();
        List<CrossServerInfo> crossServerInfoList = new ServerMgr().getCrossActivityServerDb(666);
        Map<Long, UnionActivityServerMove> serverMoveMap = new HashMap<>();
        for (ActivityInfo info : activityInfoList) {
            int activityId = info.getActivityId();
            activityMgr.initActivityConditionInfoMap(info.getActivityId());
            Set<Integer> conditionTypeSet = activityMgr.getConditionInfoMap(activityId);
            for (Integer conditionType : conditionTypeSet) {
                if (!unionRankType().contains(conditionType)) {
                    continue;
                }
                List<UserActivityConditionData> allConditionData = new ArrayList<>();
                for (Long serverId : info.getServerIdList()) {
                    if (!serverList.contains(serverId)) {
                        continue;
                    }
                    ServerInfo serverInfo = serverInfoMap.get(serverId);
                    if (serverInfo != null) {
                        UnionActivityServerMove serverMove = serverMoveMap.get(serverId);
                        if (serverMove == null) {
                             serverMove = new UnionActivityServerMove(new GameServer(serverInfo));
                            serverMoveMap.put(serverId, serverMove);
                        }
                        // 获取玩家活动数据
                        if (info.getType() == eActivityType.CommonActivity.getValue() || info.getType() == eActivityType.CrossCommonActivity.getValue()) {
                            List<UserActivityConditionData> dataList = serverMove.getCommonUserActivityConditionData(activityId, conditionType);
                            allConditionData.addAll(dataList);
                        }else {
                            Map<Long, UserActivityConditionData> dataMap = serverMove.getUserActivityConditionData(activityId, conditionType);
                            allConditionData.addAll(dataMap.values());
                        }
                    }
                }
                for (UserActivityConditionData data : allConditionData) {
                    // 插入到 跨服
                    serverMgr.addUserConditionData(data, getCrossServerInfo(info.getCrossId(), crossServerInfoList));
                }
            }
            for (Long serverId : info.getServerIdList()) {
                if (!serverList.contains(serverId)) {
                    continue;
                }
                ServerInfo serverInfo = serverInfoMap.get(serverId);
                if (serverInfo != null) {
                    UnionActivityServerMove serverMove = serverMoveMap.get(serverId);
                    if (serverMove == null) {
                        serverMove = new UnionActivityServerMove(new GameServer(serverInfo));
                        serverMoveMap.put(serverId, serverMove);
                    }
                    // 获取玩家活动数据
                    Map<String, UnionInfo> unionInfoMap = new HashMap<>();
                    List<UnionActivityConditionData> dataList = serverMove.getUnionConditionData(activityId);
                    for (UnionActivityConditionData data : dataList) {
                        if(((info.getType() == eActivityType.UnionRankActivity.getValue() && info.getChildType() ==2) ||
                                (info.getType() == eActivityType.CrossUnionActivity.getValue() && info.getChildType() == 3))) {
                            UnionInfo unionInfo = unionInfoMap.get(data.getUnionUid());
                            if (unionInfo == null) {
                                unionInfo = serverMove.getUnionInfo(data.getUnionUid());
                            }
                            if (unionInfo != null) {
                                unionInfoMap.put(unionInfo.getUnionUid(), unionInfo);
                                if (data.getActivityId() == unionInfo.getRankActivityId()) {
                                    long rankAddExp = unionInfo.getRankAddExp();
                                    data.setExtraValue(BigInteger.valueOf(rankAddExp));
                                }
                            }
                        }
                        serverMgr.addUnionConditionData(data, getCrossServerInfo(info.getCrossId(), crossServerInfoList));
                    }

                  /*  if (info.getType() == eActivityType.UnionRankActivity.getValue()) {
                        //如果是本服商会冲榜 还要生成商会榜
                        LinkedList<CrossUnionActivityRank> rankList = new LinkedList<>();
                        for (UnionActivityConditionData conditionData : dataList) {
                            CrossUnionActivityRank rank = new CrossUnionActivityRank();
                            rank.setUnionUid(conditionData.getUnionUid());
                            rank.setUnionBaseInfo(conditionData.getUnionBaseInfo());
                            rank.setValue(conditionData.getValue());
                            rank.setLastUpdateTime(conditionData.getLastUpdateTime());
                            rank.setServerId(conditionData.getUnionBaseInfo().getServerId());
                            rank.setActivityId(activityId);
                            boolean isDel = false;
                            UnionInfo unionInfo = unionInfoMap.get(conditionData.getUnionUid());
                            if (unionInfo == null || unionInfo.isDelete()) {
                                isDel = true;
                            }
                            rank.setDel(isDel);
                            rankList.add(rank);
                        }
                        rankList.sort((o1, o2) -> {
                            int result = o2.getValue().compareTo(o1.getValue());
                            if (result != 0) {
                                return result;
                            }
                            long time = o1.getLastUpdateTime() - o2.getLastUpdateTime();
                            result = time > 0 ? 1 : (time == 0 ? 0 : -1);
                            return result;
                        });

                        serverMgr.addUnionRank(rankList, getCrossServerInfo(info.getCrossId(), crossServerInfoList));
                    }*/
                }
            }
        }
        DataMgr.makeExcelData2Txt("");
        DataMgr.outText("111");

    }

    private CrossServerInfo getCrossServerInfo(Long crossId, List<CrossServerInfo> crossServerInfoList) {
        for (CrossServerInfo info : crossServerInfoList) {
            if (info.getCrossServerId() == crossId) {
                return info;
            }
        }
        return null;
    }

    private Set<Integer> unionRankType() {
        Set<Integer> set = new HashSet<>();
        set.add(2);
        set.add(3);
        set.add(39);
        set.add(40);
        set.add(41);
        set.add(61);
        set.add(62);
        set.add(65);
        set.add(69);
        set.add(88);
        set.add(90);
        set.add(1014);
        set.add(1017);
        set.add(1027);
        set.add(1029);
        set.add(1030);
        set.add(1085);
        return set;
    }
}
